#include <game.h>
#include <map.h>


int main() {
    _ioe_init();
    init_map();
    reset_avatar(0+off_x, GROUND+off_y, 120, 720);
    draw_avatar();
    draw_bg();
    int lvl = 0;
    draw_map(lvl);

    printf("Enter any key to start the game!\n");

    while (1) {
        int key = read_key();
        if (key!=_KEY_NONE) break;
    }

    int time = uptime();
    int key;

    // 主循环
    while(1) {
        time = uptime();
        
        // crash
        if (crash(lvl)) {
            undraw_avatar();
            reset_avatar(0, GROUND+off_y, avatar.vx, 720);
        }

        // draw
        undraw_avatar();
        update();
        if (hit()) {
            undraw_map(lvl);
            lvl = (lvl+1)%LVL_NUM;
            draw_map(lvl);
        }
        draw_avatar();
        draw_bg();
        draw_map(lvl);
        while ((key=my_read_key()) != _KEY_NONE) {
            if (key==_KEY_ESCAPE) _halt(0);
            if ((key)==313) jump(320);
            else if ((key)==329) jump(220);
        }
        while(uptime()<time+1000/FPS) ;
    }   
}